package com.atguigu.tingshu.user.pattern.impl;

import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.model.user.UserPaidAlbum;
import com.atguigu.tingshu.user.mapper.UserPaidAlbumMapper;
import com.atguigu.tingshu.user.pattern.DeliveryStrategy;
import com.atguigu.tingshu.vo.user.UserPaidRecordVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * ClassName : AlbumDelivery
 * Package : com.atguigu.tingshu.user.pattern.impl
 * Description :
 * <p>
 * Author: LHZ
 * Create: 2025/8/1 23:29
 * Version: 1.0
 */
@Slf4j
@SuppressWarnings({"all"})
@Component(SystemConstant.ORDER_ITEM_TYPE_ALBUM)
public class AlbumDelivery implements DeliveryStrategy {

    @Autowired
    private UserPaidAlbumMapper userPaidAlbumMapper;

    @Override
    public void delivery(UserPaidRecordVo vo) {
        //获取订单号
        String orderNo = vo.getOrderNo();
        //专辑
        //根据订单号从用户购买记录中查询,判断是否已经存在此记录
        Long count = userPaidAlbumMapper.selectCount(
                new LambdaQueryWrapper<UserPaidAlbum>()
                        .eq(UserPaidAlbum::getOrderNo, orderNo)
        );
        if(count > 0){
            log.info("已存在该订单:{}的已购专辑记录", orderNo);
            return;
        }
        //如果不存在该记录则封装对象将记录存入数据库中
        UserPaidAlbum userPaidAlbum = new UserPaidAlbum();
        userPaidAlbum.setOrderNo(orderNo);
        userPaidAlbum.setUserId(vo.getUserId());
        userPaidAlbum.setAlbumId(vo.getItemIdList().get(0));
        userPaidAlbumMapper.insert(userPaidAlbum);
    }


}